import { numberUtil } from '@youngbeen/angle-util'

export const shuffle = (toShuffle) => {
  const toShuffleArray = JSON.parse(JSON.stringify(toShuffle))
  const randomIndexs = numberUtil.multiRandom(toShuffleArray.length, toShuffleArray.length - 1, 0)
  const result = []
  randomIndexs.forEach(index => {
    result.push(toShuffleArray[index])
  })
  return result
}

export const getHeadToLocation = (from, doorIndex) => {
  // use rotate and door index to calculating the target location
  let rotateIndexChange = 0
  switch (from.rotate) {
    case 90:
      rotateIndexChange = 1
      break
    case 180:
      rotateIndexChange = 2
      break
    case 270:
      rotateIndexChange = 3
      break
    default:
      rotateIndexChange = 0
  }
  switch ((doorIndex + rotateIndexChange) % 4) {
    case 0:
      // move up
      return {
        x: from.x,
        y: from.y - 1
      }
    case 1:
      // move right
      return {
        x: from.x + 1,
        y: from.y
      }
    case 2:
      // move down
      return {
        x: from.x,
        y: from.y + 1
      }
    case 3:
      // move left
      return {
        x: from.x - 1,
        y: from.y
      }
  }
}
